var str=location.search;
str=str.split('?')[1].split('&');
class_num=str[0];
sec_num=str[1];
$('.class_num').text(class_num);
$("input[name='sec_num']").val(sec_num);

var sub_nums=new Array();
var sub_names=new Array();
if (islogin()) {
    $.post({
        url: "../../requests/teacher/select_class_name.php",
        data: {'class_num': class_num},
        async:false,
        success: function (data) {
            if (data != 'none') {
                $('.class_name').text(data);
            }
        }
    });
    $.post({
        url: '../../requests/teacher/select_sec_sub_for_edit.php',
        data: {'class_num': class_num,'sec_num':sec_num},
        async: false,
        success: function (data) {
            $("input[name='sec_name']").val(data.name);
            for (var i = 0; i < data.sub.length; i++) {
                var dom="<div class='list-block'>" +
                            "<ul>" +
                                "<li class='item-content'>" +
                                    "<div class='item-inner'>" +
                                        "<div class='item-title label'>小节序号</div>"+
                                        "<div class='item-input'>" +
                                            "<input type='number' name='sub_num' value='"+data.sub[i].num+"'>"+
                                        "</div>"+
                                    "</div>"+
                                "</li>"+
                                "<li class='item-content'>" +
                                    "<div class='item-inner'>" +
                                        "<div class='item-title label'>小节名</div>"+
                                        "<div class='item-input'>" +
                                            "<input type='text' name='sub_name' value='"+data.sub[i].name+"'>"+
                                        "</div>"+
                                    "</div>"+
                                "</li>"+
                            "</ul>"+
                        "</div>";
                $('.box').append(dom);
            }

            $("input[name='sub_num']").each(function () {
                sub_nums.push($(this).val());
            });
            $("input[name='sub_name']").each(function () {
                sub_names.push($(this).val());
            });
        }
    });


    $('#btn_add_sub').click(function () {
        var dom="<div class='list-block'>" +
            "<ul>" +
            "<li class='item-content'>" +
            "<div class='item-inner'>" +
            "<div class='item-title label'>小节序号</div>"+
            "<div class='item-input'>" +
            "<input type='number' name='sub_num_add'>"+
            "</div>"+
            "</div>"+
            "</li>"+
            "<li class='item-content'>" +
            "<div class='item-inner'>" +
            "<div class='item-title label'>小节名</div>"+
            "<div class='item-input'>" +
            "<input type='text' name='sub_name_add'>"+
            "</div>"+
            "</div>"+
            "</li>"+
            "</ul>"+
            "</div>";
        $('.box').append(dom);
    });

    $('#btn_edit').click(function () {
        edit_sec();
        edit_sub();
        add_sub();
    });
}
else window.location.href='../../index.html';
else window.location.href="../index.html";
//修改章节函数
function edit_sec() {
    if ($("input[name='sec_num']").val() != "") {
        $.post({
            url: '../../requests/teacher/edit_sec.php',
            data: {
                'class_num': class_num,
                'sec_num_old': sec_num,
                'sec_num_new': $("input[name='sec_num']").val(),
                'sec_name_new': $("input[name='sec_name']").val()
            },
            async: false,
            success: function (data) {
                 if (data == 'sec_num_exist') {
                     $$.alert('章节序号已经存在',function () {
                         location.reload(true);
                     });
                }
                else sec_num=$("input[name='sec_num']").val();
            }
        });
    }
    else $$.alert('章节序号不能为空');
}
//修改小节函数
function edit_sub() {
        var sub_nums_new = new Array();
        var sub_names_new = new Array();
        $("input[name='sub_num']").each(function () {
            if($(this).val()==""){
                $$.alert('小节序号不能为空',function () {
                    location.reload(true);
                });
            }
            sub_nums_new.push($(this).val());
        });
        $("input[name='sub_name']").each(function () {
            sub_names_new.push($(this).val());
        });
        $.post({
            url: '../../requests/teacher/edit_sub.php',
            data: {
                'sub_nums': sub_nums, 'sub_names': sub_names,
                'sub_nums_new': sub_nums_new, 'sub_names_new': sub_names_new, 'class_num': class_num, 'sec_num': sec_num
            },
            async: false,
            success: function (data) {
                if (data.indexOf('sub_exist')> 0) {
                    $$.alert('小节序号重复,已跳过');
                }
            }
        });
}

//添加小节函数
function add_sub() {
    var add_sub_names=new Array();
    var add_sub_sums=new Array();
    $("input[name='sub_num_add']").each(function () {
        add_sub_sums.push($(this).val());
    });
    $("input[name='sub_name_add']").each(function () {
        add_sub_names.push($(this).val());
    });
    $.post({
        url:'../../requests/teacher/add_sub.php',
        data:{'nums':add_sub_sums,'names':add_sub_names,'class':class_num,'sec':sec_num},
        async:false,
        success:function (data) {
            if (data.indexOf('sub_exist')> 0) {
                $$.alert('小节序号重复,已跳过',function () {
                    window.location.href='edit_course.html?'+class_num+"&"+sec_num;
                });
            }
        }
    });
}